package com.ningyue.water.Controller.persons;

import com.ningyue.water.entity.HelloTest;
import com.ningyue.water.entity.WaterMeasureRecordDO;
import com.ningyue.water.entity.common.Result;
import com.ningyue.water.mqttProcessor.MQTTHandle;
import com.ningyue.water.service.persons.PersonsService;
import com.ningyue.water.service.watermeasure.WaterMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.Date;


@Controller
@RequestMapping("person")
public class PersonsController {

	@Autowired
	private PersonsService personsService;

    @Autowired
    private WaterMeasureService waterMeasureService;



	@RequestMapping("add")
	public String addPersons( HelloTest helloTest,ModelMap model) {
		model.addAttribute("message", "Hello world!");
		personsService.addPerson(helloTest);
		return "redirect:/";
	}
	@RequestMapping("sendmsg")
	@ResponseBody
	public String sendMsg() {

        Byte b1=(byte)55;
        Byte b2=(byte)62;
        Byte b3=(byte)78;
        Byte b4=(byte)98;
        Byte b5=(byte)99;
        Byte b6=(byte)77;
        Byte b7=(byte)65;
        byte[] msg=new byte[7];
        msg[0]=b1;
        msg[1]=b2;
        msg[2]=b3;
        msg[3]=b4;
        msg[4]=b5;
        msg[5]=b6;
        msg[6]=b7;
        String message = null;
        try {
            message = new String(msg, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        MQTTHandle.sendMessage(message,"nanjing");
		return "redirect:/";
	}

	@RequestMapping("sendmessage")
	@ResponseBody
	public Result<?> sendControlMessage() {
		String message="this is one two three four!";
        WaterMeasureRecordDO waterMeasureRecordDO=new WaterMeasureRecordDO();
        waterMeasureRecordDO.setGmtCreate(new Date());
        waterMeasureRecordDO.setGmtModify(new Date());
        waterMeasureRecordDO.setDeviceId(87);
        waterMeasureRecordDO.setDataName("反渗透2压力");
        waterMeasureRecordDO.setDataType(2);
        waterMeasureRecordDO.setDataValue(6.2);
        waterMeasureRecordDO.setDataUnit("pa");
        waterMeasureRecordDO.setDeviceOwner(57);
        waterMeasureRecordDO.setIsDeleted("N");
        waterMeasureRecordDO.setDeviceCompany(null);
        int affctLine=waterMeasureService.addWaterMeasureRecord(waterMeasureRecordDO);
        if(affctLine !=1){
            return Result.wrapErrorResult("","add fail!");
        }
		return Result.wrapSuccessfulResult("ssss");
	}
}